﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using QASPRibbon.Commands;
using QASPRibbon.Helpers;
using Schema = QASPRibbon.RibbonSchema;

namespace QASPRibbon.Controls
{
    /// <summary>
    /// Anchor point for a flyout menu.
    /// </summary>
    public class RibbonFlyoutAnchor : RibbonVisualControl, IRibbonMenu
    {
        #region JavaScript

        #endregion

        #region Properties

        /// <summary>
        /// 
        /// </summary>
        [Obsolete("Use property Controls instead Sections")]
        public IEnumerable<RibbonMenuSection> Sections { get { return Controls; } set { Controls = value; } }

        /// <summary>
        /// 
        /// </summary>
        public IEnumerable<RibbonMenuSection> Controls { get; set; }

        /// <summary>
        /// An alternative text that is used for the combo box.
        /// </summary>
        public string Alt
        {
            get { return _alt.Localized(); }
            set { _alt = value; }
        }

        /// <summary>
        /// A value that indicates whether a menu will be created dynamically (True) or not (False).
        /// The default is False.
        /// </summary>
        public bool PopulateDynamically { get; set; }

        /// <summary>
        /// A value that indicates whether a dynamic menu will be populated only the first time that it is accessed. 
        /// The default is False.
        /// </summary>
        public bool PopulateOnlyOnce { get; set; }

        /// <summary>
        /// A value that indicates whether constructed versions of a dynamic menu is stored to use in next time. 
        /// The default is False.
        /// </summary>
        public bool CacheMenuVersions
        {
            get { return _cacheMenuVersions; }
            set { _cacheMenuVersions = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        public string OnClientActivate { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public string OnClientStateCheck { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public string OnClientMenuClose { get; set; }

        #endregion

        #region Internal

        internal IEnumerable<RibbonControl> AllControls()
        {
            var res = new List<RibbonControl>();
            foreach (var section in Controls.Where(s => s.Visible))
            {
                section.NameSpace = FullId + ".Menu";
                res.AddRange(section.AllControls);
                res.Add(section);
            }

            return res;
        }

        internal void SetMenuFullId(RibbonVisualControl menu)
        {
            foreach (var c in Controls)
            {
                c.SetMenuFullId(menu);
            }
        }

        internal override string XmlTag
        {
            get { return Schema.FlyoutAnchor; }
        }

        internal override IEnumerable<RibbonCommand> ClientCommands
        {
            get
            {
                var res = new List<RibbonCommand>
                    {
                        new RibbonCommand(FullId, Schema.Command, string.Empty),
                        new RibbonCommand(FullId, Schema.CheckState,
                                          string.Format("{0}; {1}; return true", OnClientActivate ?? string.Empty,
                                                        OnClientStateCheck)),
                        new RibbonCommand(FullId, Schema.CommandMenuClose, OnClientMenuClose)
                    };
                return res;
            }
        }

        internal override XElement AsXElement()
        {
            var element = base.AsXElement();
            element.Add(new XAttribute(Schema.LabelText, Title));
            element.Add(new XAttribute(Schema.TemplateAlias, TemplateAlias));
            element.Add(new XAttribute(Schema.PopulateQueryCommand, FullId + Schema.PopulateQueryCommand));
            element.Add(new XAttribute(Schema.CommandMenuClose, FullId + Schema.CommandMenuClose));
            element.Add(new XAttribute(Schema.Command, FullId + Schema.Command));
            element.Add(new XAttribute(Schema.CommandQuery, FullId + Schema.CommandQuery));
            element.Add(new XAttribute(Schema.CacheMenuVersions, CacheMenuVersions ? "true" : "false"));
            element.Add(new XAttribute(Schema.PopulateDynamically, PopulateDynamically ? "true" : "false"));
            element.Add(new XAttribute(Schema.PopulateOnlyOnce, PopulateOnlyOnce ? "true" : "false"));
            var menuElement = new XElement(Schema.Menu, new XAttribute(Schema.Id, FullId + ".Menu"));
            var sectionIndex = 0;
            foreach (var section in Controls)
            {
                sectionIndex++;
                section.Sequence = sectionIndex;
                section.NameSpace = FullId + ".Menu";
                menuElement.Add(section.AsXElement());
            }
            element.Add(menuElement);
            return element;
        }

        #endregion

        #region Constructors

        /// <summary>
        /// 
        /// </summary>
        public RibbonFlyoutAnchor() { }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        public RibbonFlyoutAnchor(string id) : base(id) { }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <param name="title"></param>
        public RibbonFlyoutAnchor(string id, string title) : base(id, title) { }

        #endregion

        #region Private

        #endregion

        #region Private fields

        private string _alt = string.Empty;
        private bool _cacheMenuVersions = true;

        #endregion
    }
}
